package com.manager.devicemanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manager.devicemanager.entity.Testbatch;
import com.manager.devicemanager.mapper.TestbatchMapper;
import com.manager.devicemanager.service.ITestbatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wpf
 * @since 2025-05-29
 */
@Service
public class TestbatchServiceImpl extends ServiceImpl<TestbatchMapper, Testbatch> implements ITestbatchService {
    @Autowired
    private TestbatchMapper testbatchMapper;

    @Override
    public IPage<Testbatch> selectPage(String dataSourceName, Page<Testbatch> page, Testbatch testbatch) {
        return testbatchMapper.selectPage(page, testbatch.getBatchname());
    }

    @Override
    public Long noendcount(String dataSourceName) {
        LambdaQueryWrapper<Testbatch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(Testbatch::getEndtime);
        return testbatchMapper.selectCount(queryWrapper);
    }


    @Override
    public void updateTestbatchStatus(String dataSourceName, Testbatch testbatch) {
        testbatchMapper.updateTestbatchStatus(testbatch);
    }

    @Override
    public void updateTestBatchStartTime(String dataSourceName, Testbatch testbatch) {
        testbatchMapper.updateTestBatchStartTime(testbatch.getBatchname(), testbatch.getStarttime());
    }

    @Override
    public void updateTestBatchEndTime(String dataSourceName, Testbatch testbatch) {
        testbatchMapper.updateTestBatchEndTime(testbatch.getBatchname(), testbatch.getEndtime());
    }

    @Override
    public Long getRunHours(String dataSourceName) {
        LambdaQueryWrapper<Testbatch> testbatchLambdaQueryWrapper = new LambdaQueryWrapper<>();
        testbatchLambdaQueryWrapper.isNull(Testbatch::getEndtime);
        testbatchLambdaQueryWrapper.isNotNull(Testbatch::getStarttime);
        testbatchLambdaQueryWrapper.orderByDesc(Testbatch::getStarttime);
        testbatchLambdaQueryWrapper.last("limit 1");
        Testbatch testbatch = testbatchMapper.selectOne(testbatchLambdaQueryWrapper);
        if (testbatch!=null){
            Date starttime = testbatch.getStarttime();
            Date endtime = new Date();
            long diff = endtime.getTime() - starttime.getTime();
            long hours = diff / (1000 * 60 * 60 );
            return hours;
        }
        return 0L;
    }
    @Override
    public Long getRunDays(String dataSourceName) {
        LambdaQueryWrapper<Testbatch> testbatchLambdaQueryWrapper = new LambdaQueryWrapper<>();
        testbatchLambdaQueryWrapper.isNull(Testbatch::getEndtime);
        testbatchLambdaQueryWrapper.isNotNull(Testbatch::getStarttime);
        testbatchLambdaQueryWrapper.orderByDesc(Testbatch::getStarttime);
        testbatchLambdaQueryWrapper.last("limit 1");
        Testbatch testbatch = testbatchMapper.selectOne(testbatchLambdaQueryWrapper);
        if (testbatch!=null){
            Date starttime = testbatch.getStarttime();
            Date endtime = new Date();
            long diff = endtime.getTime() - starttime.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            return days;
        }
        return 0L;
    }

    @Override
    public Testbatch getLastData(String dataSourceName) {
        LambdaQueryWrapper<Testbatch> testbatchLambdaQueryWrapper = new LambdaQueryWrapper<>();
        testbatchLambdaQueryWrapper.isNull(Testbatch::getEndtime);
        testbatchLambdaQueryWrapper.isNotNull(Testbatch::getStarttime);
        testbatchLambdaQueryWrapper.orderByDesc(Testbatch::getStarttime);
        testbatchLambdaQueryWrapper.last("limit 1");
        Testbatch testbatch = testbatchMapper.selectOne(testbatchLambdaQueryWrapper);
        return testbatch;
    }
}
